package com.hup.joystick.view.quickMenu;

import com.hup.joystick.service.JoyEventService;
import com.hup.joystick.view.customView.BaseMenuWindow;
import com.hup.joystick.view.quickMenu.QuickMenu.ItemHandler;
import com.hup.utils.commons.exception.AppCommonException;
import com.hup.utils.commons.io.FileUtil;
import com.hup.utils.commons.start.DialogHelper;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.swing.ComponentUtil;
import com.hup.utils.swing.customView.ImageButton;
import sun.awt.shell.ShellFolder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * 程序item类
 *
 * @author hugan
 * @date 2020/11/12
 */
public class QMAppItem extends ImageButton implements ActionListener {
    private final ImageIcon LOST_ICON = new ImageIcon(QMAppItem.class.getResource("/img/icon_lost.png"));

    private String filePath;
    private Throwable err;

    public QMAppItem() {
        setUnitedIconSize(3, 3);
        setBackground(BaseMenuWindow.TRANSPARENT_MENU_BG);
        setOpaque(true);
        setFont(LookThemeUtil.getDefaultScaledFont().deriveFont(Font.BOLD));
        setActionListener(this);
    }

    private void initData(String filePath) {
        File file = new File(filePath);
        ImageIcon icon;
        String nameText;
        String tooltip;
        try {
            ShellFolder sf = FileUtil.getShellFolder(file);
            if (sf.isLink()) sf = FileUtil.getLinkLocation(sf);
            Image image = sf.getIcon(true);
            icon = image != null ? new ImageIcon(image, sf.getFolderType()) : LOST_ICON;
            nameText = sf.getName();
            tooltip = sf.getAbsolutePath();
            err = null;
        } catch (FileNotFoundException e) {
            icon = LOST_ICON;
            nameText = "找不到:" + file.getName();
            tooltip = filePath;
            err = new AppCommonException("找不到:\n" + filePath);
        } catch (Exception e) {
            icon = LOST_ICON;
            nameText = "解析错误:" + file.getName();
            tooltip = filePath;
            err = new AppCommonException("解析错误:\n"
                    + filePath + "\n"
                    + e.getMessage()
            );
        }
        setText(nameText);
        setToolTipText(tooltip);
        setIcon(icon);
        this.filePath = tooltip;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (err != null) {
            DialogHelper.show(null, err, ComponentUtil.getParentWindow(this));
        } else {
            ComponentUtil.getParentWindow(this).dispose();
            SpringContext.getBean(JoyEventService.class).runApp(filePath);
        }
    }

    public static class QMAppItemHandler implements ItemHandler<QMAppItem, String> {

        @Override
        public QMAppItem createItem() {
            return new QMAppItem();
        }

        @Override
        public void initData(QMAppItem item, String filePath) {
            item.initData(filePath);
        }
    }

}
